$(function(){
	refresh();
	//setInterval(getData, 15000);
});

var toindites = new Toindites();
mui.plusReady(toindites.dealHref);

function refresh(){
	getData();
	getWeather();
}

function getData(){
	$.proAjax({
		url:"/mobile/energy",
		data:{home:"home"},
		dataType:"json",
		success:function(data){
			var arr=['pscount','acpower','capacity'];
			for( var key in data){
				if(data[key]>10000 && $.inArray(key,arr)==-1){
						$("#"+key).text((data[key]/10000||0).toFixed(2));
						$("#"+key).next("span:contains('万')").remove();
						$("#"+key).after(" <span>万</span>");
				}else if(key=='acpower'){
					if(data[key]>1000){
						$("#"+key).text((data[key]/1000||0).toFixed(2));
						$("#"+key).next().html("<span>MW</span>");
					}else{
						$("#"+key).text((data[key]||0).toFixed(2));
						$("#"+key).next().html("<span>kW</span>");
					}
				}else if(key==arr[0]){
					$("#"+key).text((data[key]||0).toFixed(0));
				}else if(key=='capacity'){
					if(data[key]>50){
						$("#"+key).text((data[key]/1000||0).toFixed(2));
						$("#"+key).next().html("<span>MW</span>");
					}else{
						$("#"+key).text((data[key]||0).toFixed(2));
						$("#"+key).next().html("<span>kW</span>");
					}
				}else{
					$("#"+key).text((data[key]||0).toFixed(2));
					$("#"+key).next("span:contains('万')").remove();
				}
			}
			toindites.dealHref();
		}
			
		
	});
}
function getWeather(){
	$.proAjax({
		url:'/mobile/loginweather',
		dataType:'json',
		success:function(weather){
			var pinyin = weather.pinyin || "weizhi";
//			$("#cityname").text(weather.cityname);
			$("#pinyin").attr("src","../weatherImg/tq-"+pinyin+".png");
			$("#template").text(weather.template+"℃");
		}
	});
}

//获取当前城市
function local() {
	//IOS
	mui.plusReady(function(){ 
		plus.geolocation.getCurrentPosition( geoInf, function (e) {
			locaError();
		},{geocode:true,provider:'amap'});
	});
	
	//Android
	if (navigator.geolocation){
		navigator.geolocation.getCurrentPosition(geoInf,locaError);
	}else{
		mui.alert("你的浏览器不支持地理定位!");
	}
	//获取具体的定位信息
	function geoInf(position) {
		$("#cityname").text(position.address.city);
	}
	//部分机型缺少GMS服务包，导致失败时调用
	function locaError() {
		$.proAjax({
			url:'/mobile/loginweather',
			dataType:'json',
			success:function(weather){
				$("#cityname").text(weather.cityname);
			}
		});
	}
}

local();
setInterval(function(){ 
	local(); 
}, 30000);
